package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.WebLogParam;
import com.project.hqshorttaxi.entity.WebLog;
import com.project.hqshorttaxi.mapper.WebLogMapper;
import com.project.hqshorttaxi.service.WebLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WebLogServiceImpl implements WebLogService {

    @Resource
    private WebLogMapper webLogMapper;

    @Override
    public WebLog getById(Integer id) {
        return webLogMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(WebLog entity) throws Exception {
        webLogMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(WebLog entity) throws Exception {
        webLogMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        webLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<WebLog> list(WebLogParam param) {
        Example example = new Example(WebLog.class);
        Example.Criteria c = example.createCriteria();
        if (param.getOperation() != null) {
            c.andEqualTo("operation", param.getOperation());
        }
        if (!StringUtils.isEmpty(param.getCreateUser())) {
            c.andLike("createUser", "%" + param.getCreateUser() + "%");
        }
        if (!StringUtils.isEmpty(param.getContent())) {
            c.andLike("content", "%" + param.getContent() + "%");
        }
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("createTime", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("createTime", param.getEndTime());
        }
        example.orderBy("createTime").desc();
        List<WebLog> list = webLogMapper.selectByExample(example);
        return list;
    }
}
